<template>
  <div style="padding: 20px;">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="放置条件" name="first">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="100px"
          >
            <div>
              <el-form-item label="适用考察类型">
                <el-select
                  style="width: 400px"
                  v-model="queryForm.query"
                  placeholder="考察类型"
                  clearable
                  size="small"
                  @change="handleQuery"
                >
                  <el-option
                    v-for="(item, index) in values"
                    :key="index"
                    :label="item.inspect_type_name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType"
                  >添加放置条件</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div>
          <el-table :data="data" size="medium" border>
            <el-table-column
              label="稳定性放置条件"
              align="center"
              prop="place_conditions_name"
            ></el-table-column>
             <el-table-column
              label="放置条件类型"
              align="center"
              prop="place_conditions_type"
            >
            <template slot-scope="scope">
              <div v-if='scope.row.place_conditions_type'>
                {{scope.row.place_conditions_type.place_conditions_type_name}}
              </div>
            </template>
            </el-table-column>
            <el-table-column
              label="适用考察类型"
              align="center"
              prop="resource"
            >
            <template slot-scope="scope">
              <div style="display: flex;flex-wrap: wrap;justify-content:center">
                <div v-for="(item,index) in scope.row.relation_list" :key=index>
                  {{item.inspect_type_name}}
                  <span v-if="scope.row.relation_list.length-1!=index">,</span>
                </div>
              </div>
            </template>
            </el-table-column>
            <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
              <template slot-scope="scope">
                <el-button type="text"  size="meduim" @click="editCondition(scope.row)"
                  >编辑
                </el-button>
                <el-button
                  class="text-btn-red"
                  type="text"
                  size="meduim"
                  @click="deleteItem(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total"
          :page.sync="pages.pageNum" :limit.sync="pages.pageSize" @pagination="afreshList" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="放置条件类型" name="second">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="68px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name"
                  placeholder="请输入放置条件类型"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery1"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addCtype"
                  >添加放置条件类型</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <el-table :data="data1" size="medium" border>
          <el-table-column
            prop="place_conditions_type_name"
            label="放置条件类型"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
            <template slot-scope="scope">
              <el-button type="text"  size="meduim"  @click="editCondition1(scope.row)"
                  >编辑
              </el-button>
              <el-button
                class="text-btn-red"
                type="text"
                size="meduim"
                @click="deleteItem1(scope.row, scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total1"
          :page.sync="pages1.pageNum" :limit.sync="pages1.pageSize" @pagination="afreshList1" />
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="放置条件" prop='name'>
          <el-input v-model="form.name" placeholder="请输入放置条件" />
        </el-form-item>
         <el-form-item label="放置条件类型" prop="fType">
          <el-select
            style="width: 100%"
            v-model="form.fType"
            placeholder="放置条件类型"
            clearable
            size="small"
            @change="selectfType"
          >
            <el-option
              v-for="(item, index) in values1"
              :key="index"
              :label="item.place_conditions_type_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="适用考察类型" prop="type">
          <el-select
            style="width: 100%"
            v-model="form.type"
            placeholder="考察类型"
            clearable
            multiple
            size="small"
            @change="selectType"
          >
            <el-option
              v-for="(item, index) in values"
              :key="index"
              :label="item.inspect_type_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose">取消</el-button>
        <el-button plain v-if="title=='添加放置条件'" @click="cSubmitForm">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title1"
      :visible.sync="open1"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="140px">
        <el-form-item label="放置条件类型" prop='name1'>
           <el-input v-model="form1.name1" placeholder="请输入放置条件类型"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose">取消</el-button>
        <el-button plain v-if="title1=='添加放置条件类型'" @click="cSubmitForm1">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm1">确定</el-button>
      </div>
    </el-dialog>      
  </div>
</template>

<script>
import {selectInvestigationList,createCondition,conditionList,updateCondition,deleteCondition,selectInvestigationfList,conditionTypeList,createTypeCondition,updateTypeCondition,deleteTypeCondition} from "@/api/stability";
export default {
  data() {
    let name = (rule, value, callback) => {
      if (this.form.name != '') {
        if(this.form.name.split(" ").join("").length == 0){
          callback(new Error("放置条件名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("放置条件名称不能为空"));
      }
    };
    let name1 = (rule, value, callback) => {
      if (this.form1.name1 != '') {
        if(this.form1.name1.split(" ").join("").length == 0){
          callback(new Error("放置条件类型名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("放置条件类型名称不能为空"));
      }
    };
    let fType = (rule, value, callback) => {
      if (this.form.fType != '') {
        callback();
      } else {
        callback(new Error("请选择放置类型"));
      }
    };
    let type1 = (rule, value, callback) => {
      if (this.form.type.length > 0) {
        callback();
      } else {
        callback(new Error("请至少选择一个考察类型"));
      }
    };
    return {
      title:'添加放置条件',
      title1:'添加放置条件类型',
      pages:{
        pageNum:1,
        pageSize:10

      },
      pages1:{
        pageNum:1,
        pageSize:10

      },
      total:0,
      total1:0,
      activeName: "first",
      queryForm: {
        query:'',
        type: [],
        types: [],
        name: "",
        name1:'',
        fType:[]
      },
      current: null,
      isEdit: false,
      values: [],
      values1: [],
      current: null,
      data: [
        
      ],
      data1: [
        
      ],
      open: false,
      open1: false,
      form: {
        type: [],
        name:'',
        fType:"",
        name1:''
      },
      form1:{
        name1:''
      },
      rules1:{
        name1: [
          { required: true, validator: name1, trigger: "blur" },
        ],
      },
      rules: {
        name: [
          { required: true, validator: name, trigger: "blur" },
        ],
        // name1: [
        //   { required: true, validator: name1, trigger: "blur" },
        // ],
        fType: [
          { required: true,validator: fType },
        ],
        type: [
          { required: true,validator: type1},
        ],
      },
      relation_list:[],
      created_at: "",
      creator_id: "",
      creator_name: "",
      deleted_at: "",
      id: "",
      updated_at: "",
      place_conditions_type:{}
    };
  },
  created() {
    this.getSelectType()
    this.getSelectfType()
    this.getConditionList()
    this.getConditionTypeList()
  },
  methods: {
    handleQuery(){
      this.pages={
        pageNum:1,
        pageSize:10
      },
      this.getConditionList()
    },
    handleQuery1(){
      this.pages1={
        pageNum:1,
        pageSize:10

      },
      this.getConditionTypeList()
    },
      //获取考察类型
    getSelectType(){
      selectInvestigationList().then(res=>{
        if(res.code==200){
          console.log(res.data)
          this.values = res.data
        }
      })
    },
    //获取放置条件下拉
    getSelectfType(){
      selectInvestigationfList().then(res=>{
        if(res.code==200){
          console.log(res.data)
          this.values1 = res.data
        }
      })
    },
    getConditionList(){
      let data = {
        inspect_type_id:this.queryForm.query,
        pager:{
          limit:this.pages.pageSize,
          skip:this.pages.pageNum
        }
      }
       conditionList(data).then(res=>{
          if(res.code==200){
            this.data = res.data.data
            this.total = res.data.total
          }
       })
    },
    getConditionTypeList(){
      let data = {
        name:this.queryForm.name,
        pager:{
          limit:this.pages1.pageSize,
          skip:this.pages1.pageNum
        }
      }
       conditionTypeList(data).then(res=>{
          if(res.code==200){
            this.data1 = res.data.data
            this.total1 = res.data.total
          }
       })
    },
    selectType(){
      this.relation_list=[]
      this.form.type.forEach(item=>{
        this.values.forEach(itm=>{
          if(item ==itm.id){
            let obj = {
              inspect_type_id:itm.id,
              inspect_type_name:itm.inspect_type_name
            }
            this.relation_list.push(obj)
          }
        })
      })
    },
    selectfType(){
      this.place_conditions_type={}
      this.values1.forEach(itm=>{
          if(this.form.fType ==itm.id){
           this.place_conditions_type_name = {
              id:itm.id,
              place_conditions_type_name:itm.place_conditions_type_name
            }
          }
        })
        console.log(this.place_conditions_type_name)
    },
    addType() {
        this.open = true
        this.title = '添加放置条件'

       
    },
    editCondition(row){
       this.open = true
       this.title = '编辑放置条件'
       console.log(row)
      this.form.type =[]
      this.created_at=row.created_at
      this.creator_id=row.creator_id
      this.creator_name=row.creator_name
      this.deleted_at=row.deleted_at
      this.id=row.id
      this.updated_at=row.updated_at
      this.form.name = row.place_conditions_name
      this.relation_list =row.relation_list
      row.relation_list.forEach(item=>{
        this.form.type.push(item.inspect_type_id)
      })
      if(row.place_conditions_type){
        this.form.fType=row.place_conditions_type.id
      }
      this.place_conditions_type_name={

      }
      this.place_conditions_type_name = {
        id:row.place_conditions_type.id,
        place_conditions_type_name:row.place_conditions_type.name
      } 
    },
    deleteItem(row,index){
       this.$confirm("是否确认删除 " + row.place_conditions_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            deleteCondition(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getConditionList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    addCtype() {
        this.open1 = true
        this.form.name1 = ''
        this.title1 = "添加放置条件类型"
    },
    editCondition1(row){
       this.open1 = true
       this.title1 = '编辑放置条件类型'
       this.form1.name1 = row.place_conditions_type_name
       this.created_at=row.created_at
       this.creator_id=row.creator_id
       this.creator_name=row.creator_name
       this.deleted_at=row.deleted_at
       this.id=row.id
       this.updated_at=row.updated_at
    },
    deleteItem1(row,index){
       this.$confirm("是否确认删除 " + row.place_conditions_type_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            deleteTypeCondition(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getConditionTypeList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    afreshList(){
      this.getConditionList()
    },
    afreshList1(){
      this.getConditionTypeList()
    },
    handleClose() {
        this.open = false
        this.queryForm.type = []
        this.open1 = false
        this.form1.name1=''
        this.form={
          type: [],
          name:'',
          fType:"",
          name1:''
       }
    },
    cSubmitForm1(){
       this.$refs.form1.validate((valid) => {
        if (valid) {
          let data = {
            place_conditions_type_name:this.form1.name1,
          }
          createTypeCondition(data).then(res=>{
            if(res.code==200){
              this.$message.success('添加成功')
              this.form1.name1=''
              this.getConditionTypeList()
            }else{
                  this.$message.error(res.msg)
            }
          })
          }
      })
    },
    cSubmitForm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
            if(this.title =='添加放置条件'){
             let data = {
               place_conditions_name:this.form.name,
               relation_list:this.relation_list,
               place_conditions_type:this.place_conditions_type_name
            }
            createCondition(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.form={
                    type: [],
                    name:'',
                    fType:"",
                    name1:''
                }
                this.form1.name1=''
                this.queryForm.type = []
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('fType')
                  this.$refs.form.clearValidate('type')
                })
                // this.$refs['form'].clearValidate('fType');
                this.getConditionList()
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    submitForm() {
      console.log(this.place_conditions_type_name)
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.title=="添加放置条件"){
            let data = {
              place_conditions_name:this.form.name,
              relation_list:this.relation_list,
              place_conditions_type:this.place_conditions_type_name
            }
            createCondition(data).then(res=>{
              if(res.code==200){
                this.open = false
                this.$message.success('添加成功')
                this.getConditionList()
                this.handleClose()
              }else{
                  this.$message.error(res.msg)
              }
            })
          }else{
            let data = {
              id:this.id,
              created_at: this.created_at,
              creator_id: this.creator_id,
              creator_name: this.creator_name,
              deleted_at: this.deleted_at,
              updated_at: this.updated_at,
              place_conditions_name:this.form.name,
              relation_list:this.relation_list,
              place_conditions_type:this.place_conditions_type_name
            }
            updateCondition(data).then(res=>{
              if(res.code==200){
                this.open = false
                this.$message.success('编辑成功')
                this.getConditionList()
                this.handleClose()
              }else{
                this.$message.error(res.msg)
              }
            })
          }
            }
      })
    },
    submitForm1() {
      this.$refs.form1.validate((valid) => {
        if (valid) {
          if(this.title1=="添加放置条件类型"){
        let data = {
          place_conditions_type_name:this.form1.name1,
        }
        createTypeCondition(data).then(res=>{
          if(res.code==200){
            this.open1 = false
            this.$message.success('添加成功')
            this.getConditionTypeList()
            this.handleClose()
          }else{
                this.$message.error(res.msg)
           }
        })
      }else{
        let data = {
          id:this.id,
          created_at: this.created_at,
          creator_id: this.creator_id,
          creator_name: this.creator_name,
          deleted_at: this.deleted_at,
          updated_at: this.updated_at,
          place_conditions_type_name:this.form1.name1,
        }
        updateTypeCondition(data).then(res=>{
          if(res.code==200){
            this.open1 = false
            this.$message.success('编辑成功')
            this.getConditionTypeList()
          }else{
                this.$message.error(res.msg)
          }
        })
      }
        }
      })
    },
    handleClick() {
       this.getSelectfType()
    },
  },
};
</script>
<style lang="scss">
 
</style>